//
//  lxGoodsDetailViewController.m
//  luodansc
//
//  Created by 倫 星 on 2012/10/09.
//  Copyright (c) 2012年 方正株式会社. All rights reserved.
//

#import "lxGoodsDetailViewController.h"
#import "lxGoodsDetailView.h"
#import "lxWeiBoEntity.h"

@interface lxGoodsDetailViewController ()

@end

@implementation lxGoodsDetailViewController
@synthesize lxGDV = _lxGDV;
@synthesize lxGE = _lxGE;

- (void)subViewDidLoad {
    
    _goods_name = NSLocalizedString(@"goods_details", nil);
    
    _lxGE = [[lxGoodsDetailEntity alloc] init];
    self.is_supportShakeToEdit = YES;
    self.is_supportHandleSwipeFrom = YES;
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.lxGDV = [[lxGoodsDetailView alloc] initWithFrame:self.view.bounds];
    // Initialization code
    self.lxGDV.backgroundColor = [UIColor clearColor];
    
    //    self.view = _lxGDV;
    [self.view addSubview:self.lxGDV];
    
    [_arrayMenuImageList addObject:@"story-place.png"];
    [_arrayMenuImageList addObject:@"story-reload.png"];
    [_arrayMenuImageList addObject:@"story-thought.png"];
    [_arrayMenuImageList addObject:@"story-mail.png"];
    [_arrayMenuImageList addObject:@"story-camera.png"];
    [_arrayMenuImageList addObject:@"story-home.png"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
    self.title = _goods_name;
    UIBarButtonItem *reflash_bt = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"reflash_bt", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(leftReflashBtClick:)];
    self.reFlash_bt = reflash_bt;
    [reflash_bt release];
}

- (void)leftReflashBtClick:(id)sender {
    [self reFlash];
}

- (void)reFlash {
    [self sendRequest];
    self.is_loading = YES;
}

- (BOOL)refreshData {
    BOOL result = [super refreshData:_lxGE];
    if (NO == result) {
        return result;
    }
    
    result = [self.lxGDV refreshData:_lxGE];
    
    return result;
}


- (void)loadView {
	[super loadView];
}

- (void)dealloc {
    [self.lxGDV release];
    [_lxGE release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark ASIHTTPRequestDelegate Methods
- (ASIHTTPRequest *)sendRequest {
    lxAPPContainer *appContainer = [lxAPPContainer sharedLxAPPContainer];
    if (NO == [appContainer isExistNet]) {
        return nil;
    }

    NSString *str_url = [NSString stringWithFormat:@"%@%@?goods_id=%d", service_url, getGoodsServlet, _lxGE.goods_id];
    NSURL *url = [NSURL URLWithString:str_url];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request startAsynchronous];
    return request;
}

- (void)getResponse:(ASIHTTPRequest *)request {
    NSString *responseString = [request responseString];
    
    if (![responseString isKindOfClass:[NSString class]]) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    if ([lxStringUtility isNil:responseString]) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    NSData *reponsedata = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    
    if (nil == reponsedata) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    if (![reponsedata isKindOfClass:[NSData class]]) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    __autoreleasing NSError *error = nil;
    
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:reponsedata options:kNilOptions error:&error];
    if (nil == dictionary) {
        NSLog(@"Response failed because:%@", [error localizedDescription]);
        return;
    }
    
    if (![dictionary isKindOfClass:[NSDictionary class]]) {
        NSLog(@"Response failed because:%@", [error localizedDescription]);
        return;
    }
    
    [_lxGE setEntity:dictionary];
    
    if (![lxStringUtility isNil:_lxGE.goods_name]) {
        _goods_name = _lxGE.goods_name;
        [_goods_name retain];
        self.title = _goods_name;
    }
    
    return;
}

- (void)requestFinished:(ASIHTTPRequest *)request {
    [self getResponse:request];
    [self refreshData];
    self.is_loading = NO;
}

- (void)requestFailed:(ASIHTTPRequest *)request {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"error", nil)
                                                    message:NSLocalizedString(@"network_conn_failed", nil)
                                                   delegate:self
                                          cancelButtonTitle:NSLocalizedString(@"ok", nil)
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    self.is_loading = NO;
}

- (lxWeiBoEntity *)getCurrentInfo {
    lxWeiBoEntity *lxWBE = [[lxWeiBoEntity alloc] init];
    
    BOOL result = [lxWBE setEntity:_lxGE.goods_img goods_name:_lxGE.goods_name f_shop_price:_lxGE.shop_price];
    
    if (NO == result) {
        return nil;
    }
    
    return lxWBE;
}
@end

